﻿
using IDAdmin.Lib.DataLayer;
using IDAdmin.Lib.UI;
using IDAdmin.Lib.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IDAdmin.Pages
{
    public partial class PremiumLoginStatistics : BasePage
    {
        public PremiumLoginStatistics()
            : base(Lib.AppFunctions.PREMIUMLOGIN)
        {
        }
        protected override void Page_Load(object sender, EventArgs e)
        {
            if (!IsLogin())
            {
                RedirectToLogOn();
            }
            else if (!CheckRight())
            {
                RedirectToDeniedMessage();
            }
        }

        protected void countButton_Click(object sender, EventArgs e)
        {
            DateTime dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            dataPanel.Controls.Clear();
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            dataPanel.Controls.Add(getPremiumLoginStatistics(WebDB.getGoldSaveData(dateTimeList), WebDB.getScoredCardSaveData(dateTimeList), WebDB.getUsingGoldData(dateTimeList), WebDB.getGoldExchangeFromScoredCardData(dateTimeList), WebDB.getItemBuyingData(dateTimeList, "All"), WebDB.getFilledCardData(dateTimeList), WebDB.getAu(dateTimeList)));
            watch.Stop();
            double elapsedMS = watch.ElapsedMilliseconds;
            measureLB.Text = elapsedMS.ToString() + "miliseconds";
        }

        public static void cronPremiumLoginFromDateToDate(List<DateTime> dateTimeList)
        {
            List<PremiumLogin> plList = new List<PremiumLogin>();
            PremiumLoginWebDB plwebdb = new PremiumLoginWebDB();
            foreach (DateTime date in dateTimeList)
            {
                PremiumLogin pl = new PremiumLogin();

                //DateTime date = DataToolUtil.convertStringToDateTime(txtCountDate.Text);
                pl.InventoryAccountSum = plwebdb.getInventoryAccountSum(date);
                String AciveSpendAccountNum = plwebdb.getActiveSpendAccountNum(date);
                pl.ActiveSpendAccountNum = AciveSpendAccountNum;
                pl.InventoryGoldNum = plwebdb.getInventoryGoldNum(date);
                pl.InventoryScoreCardNum = plwebdb.getInventoryScoreCardNum(date);
                pl.UsingGoldNum = plwebdb.getUsingGoldNum(date);
                pl.RechargeSum = plwebdb.getRechardSum(date);
                pl.RechargeSumGosu = plwebdb.getGosu(date);
                pl.GoldSumExchangeFromScoredCard = plwebdb.getGoldExchangeFromScoredCardData(date);
                pl.UsingActualCollect = (double.Parse(pl.UsingGoldNum) / 50).ToString();
                pl.RechardAccountNum = plwebdb.getRechardAccountNum(date);
                pl.NewIncreaseRechardAccountNum = plwebdb.getNewIncreaseRechardAccountNum(date);
                pl.UsingCharacterNum = plwebdb.getUsingCharacterNum(date);
                pl.RechardAccountSum = plwebdb.getRechardAccountSum(date);
                string out1 = "";
                string out2 = "";
                plwebdb.getSevenDaySaveAccountNum(date, out out1, out out2);
                pl.SevenDaySaveAccountNum = out1;
                pl.SavePercent = out2;
                string out3 = "";
                string out4 = "";
                plwebdb.getNewIncreaseAccountLossSum(date, out out3, out out4);
                pl.NewIncreaseAccountLossSum = out3;
                pl.NewIncreaseAccountLossPercent = out4;
                pl.Date = date.ToString("dd/MM/yyyy");
                pl.DateTime = date;
                pl.Ticks = date.Ticks;
                plList.Add(pl);
            }
            plwebdb.insertPremiumLogin(plList, dateTimeList);
        }
        public static void cronPremiumLogin(DateTime date)
        {
            List<PremiumLogin> plList = new List<PremiumLogin>();
            PremiumLoginWebDB plwebdb = new PremiumLoginWebDB();
            PremiumLogin pl = new PremiumLogin();
            List<DateTime> dateTimeList = new List<DateTime>();
            dateTimeList.Add(date);
            //DateTime date = DataToolUtil.convertStringToDateTime(txtCountDate.Text);
            pl.InventoryAccountSum = plwebdb.getInventoryAccountSum(date);
            String AciveSpendAccountNum = plwebdb.getActiveSpendAccountNum(date);
            pl.ActiveSpendAccountNum = AciveSpendAccountNum;
            pl.InventoryGoldNum = plwebdb.getInventoryGoldNum(date);
            pl.InventoryScoreCardNum = plwebdb.getInventoryScoreCardNum(date);
            pl.UsingGoldNum = plwebdb.getUsingGoldNum(date);
            pl.RechargeSum = plwebdb.getRechardSum(date);
            pl.RechargeSumGosu = plwebdb.getGosu(date);
            pl.GoldSumExchangeFromScoredCard = plwebdb.getGoldExchangeFromScoredCardData(date);
            pl.UsingActualCollect = pl.UsingActualCollect = (double.Parse(pl.UsingGoldNum) / 50).ToString();
            pl.RechardAccountNum = plwebdb.getRechardAccountNum(date);
            pl.NewIncreaseRechardAccountNum = plwebdb.getNewIncreaseRechardAccountNum(date);
            pl.UsingCharacterNum = plwebdb.getUsingCharacterNum(date);
            pl.RechardAccountSum = plwebdb.getRechardAccountSum(date);
            string out1 = "";
            string out2 = "";
            plwebdb.getSevenDaySaveAccountNum(date, out out1, out out2);
            pl.SevenDaySaveAccountNum = out1;
            pl.SavePercent = out2;
            string out3 = "";
            string out4 = "";
            plwebdb.getNewIncreaseAccountLossSum(date, out out3, out out4);
            pl.NewIncreaseAccountLossSum = out3;
            pl.NewIncreaseAccountLossPercent = out4;
            pl.Date = date.ToString("dd/MM/yyyy");
            pl.DateTime = date;
            pl.Ticks = date.Ticks;
            plList.Add(pl);
            plwebdb.insertPremiumLogin(plList, dateTimeList);
        }

        private Table getPremiumLoginTable()
        {

            List<PremiumLogin> plList = new List<PremiumLogin>();

            DateTime dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            //dateTimeList = dateTimeList.ToList();
            PremiumLoginWebDB plwebdb = new PremiumLoginWebDB();
            foreach (DateTime date in dateTimeList)
            {
                PremiumLogin pl = new PremiumLogin();

                //DateTime date = DataToolUtil.convertStringToDateTime(txtCountDate.Text);
                pl.InventoryAccountSum = plwebdb.getInventoryAccountSum(date);
                String AciveSpendAccountNum = plwebdb.getActiveSpendAccountNum(date);
                pl.ActiveSpendAccountNum = AciveSpendAccountNum;
                pl.InventoryGoldNum = plwebdb.getInventoryGoldNum(date);
                pl.InventoryScoreCardNum = plwebdb.getInventoryScoreCardNum(date);
                pl.UsingGoldNum = plwebdb.getUsingGoldNum(date);
                pl.RechargeSum = plwebdb.getRechardSum(date);
                pl.RechargeSumGosu = plwebdb.getGosu(date); //getRechardSumGOSU
                pl.GoldSumExchangeFromScoredCard = plwebdb.getGoldExchangeFromScoredCardData(date);
                pl.UsingActualCollect = (double.Parse(pl.UsingGoldNum) / 50).ToString();
                pl.RechardAccountNum = plwebdb.getRechardAccountNum(date);
                pl.NewIncreaseRechardAccountNum = plwebdb.getNewIncreaseRechardAccountNum(date);
                pl.UsingCharacterNum = plwebdb.getUsingCharacterNum(date);
                pl.RechardAccountSum = plwebdb.getRechardAccountSum(date);
                string out1 = "";
                string out2 = "";
                plwebdb.getSevenDaySaveAccountNum(date, out out1, out out2);
                pl.SevenDaySaveAccountNum = out1;
                pl.SavePercent = out2;
                string out3 = "";
                string out4 = "";
                plwebdb.getNewIncreaseAccountLossSum(date, out out3, out out4);
                pl.NewIncreaseAccountLossSum = out3;
                pl.NewIncreaseAccountLossPercent = out4;
                pl.Date = date.ToString("dd/MM/yyyy");
                pl.DateTime = date;
                pl.Ticks = date.Ticks;
                plList.Add(pl);
            }
            plList = plList.OrderByDescending(o => o.Ticks).ToList();
            plwebdb.insertPremiumLogin(plList, dateTimeList);


            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Thời gian",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Tổng tài khoản tồn kho",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số tài khoản tiêu phí active tháng",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số vàng tồn kho hiện tại",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số phiếu điểm tồn kho trong ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng phiếu điểm mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Số GOSU Nạp vào",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số vàng đổi từ phiếu điểm mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    
                      UIHelpers.CreateTableCell("Số vàng tiêu hao mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                      UIHelpers.CreateTableCell("Thực thu tiêu hao mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
            UIHelpers.CreateTableCell("Số tài khoản nạp trẻ trong ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
            UIHelpers.CreateTableCell("Số tài khoản nạp trẻ tăng mới mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                UIHelpers.CreateTableCell("Số nhân vật tiêu hao trong ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng tài khoản nạp thể đến hiện tại",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số tài khoản lưu lại 7 ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tỷ lệ lưu lại",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Số thất thoát của tài khoản tăng mới",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                      UIHelpers.CreateTableCell("Tỷ lệ thất thoát của tài khoản tăng mới",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader")
                }
            );

            table.Rows.Add(rowHeader);
            try
            {
                if (plList == null || plList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    foreach (var dr in plList)
                    {
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Date),HorizontalAlign.Center,css),
                                     UIHelpers.CreateTableCell(Converter.ToString(dr.InventoryAccountSum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.ActiveSpendAccountNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.InventoryGoldNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.InventoryScoreCardNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.RechargeSum),HorizontalAlign.Center,css),
                                     UIHelpers.CreateTableCell(Converter.ToString(dr.RechargeSumGosu),HorizontalAlign.Center,css),
                                     UIHelpers.CreateTableCell(Converter.ToString(dr.GoldSumExchangeFromScoredCard),HorizontalAlign.Center,css),
                                      UIHelpers.CreateTableCell(Converter.ToString(dr.UsingGoldNum),HorizontalAlign.Center,css),
                                      UIHelpers.CreateTableCell(Converter.ToString(dr.UsingActualCollect),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.RechardAccountNum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.NewIncreaseRechardAccountNum),HorizontalAlign.Center,css),
                         UIHelpers.CreateTableCell(Converter.ToString(dr.UsingCharacterNum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.RechardAccountSum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.SevenDaySaveAccountNum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.SavePercent),HorizontalAlign.Center,css),
                         UIHelpers.CreateTableCell(Converter.ToString(dr.NewIncreaseAccountLossSum),HorizontalAlign.Center,css),
                          UIHelpers.CreateTableCell(Converter.ToString(dr.NewIncreaseAccountLossPercent),HorizontalAlign.Center,css)
                        }
                        );

                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }
        private Table getPremiumLoginStatistics(List<GoldSave> goldSaveData, List<ScoredCardSave> scoredCardSaveData, List<OriginGoldShopData> usingGoldSumData, List<OriginGoldShopData> goldExchangeFromScoredCardData, List<OriginGoldShopData> itemPurchasingData, List<FilledCard> filledCardData, List<Au> auData)
        {
            //tmpInfo like '-' and srcid=0 and objid=0 and bind_or_not like 'b'
            // List<OriginGoldShopData>  itemConsumingData = goldShopData.Where(w => w.TmpInfo.Equals("-") && w.ScrId.Equals("0") && w.ItemId.Equals("0") && (w.Type.Equals("0") || w.Type.Equals("1")) && (w.Bing_Or_Not.Equals("b") || w.Bing_Or_Not.Equals("n"))).ToList();
            IList<PremiumLogin> plList = new List<PremiumLogin>();


            //foreach (Au auItem in auData)
            //{
            //    string timeStamp = ConverterData.truncateTime(auItem.Timestamp, "dd/MM/yyyy");
            //    auItem.Date = ConverterData.kindUnixTimeStampToKindDateTime(double.Parse(timeStamp), DateTimeKind.Utc, DateTimeKind.Local).ToString("dd/MM/yyyy");
            //    auItem.DateTime = ConverterData.kindUnixTimeStampToKindDateTime(long.Parse(timeStamp), DateTimeKind.Local, DateTimeKind.Local);
            //}

            List<DateTime> timeList = new List<DateTime>();
            DateTime dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            foreach (DateTime item in dateTimeList)
            {

                string date = item.ToString("dd/MM/yyyy");
                timeList.Add(DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture));
            }
            timeList = timeList.OrderByDescending(o => o).ToList();

            foreach (var item in timeList)
            {
                PremiumLogin pl = new PremiumLogin();
                DateTime firstDate = new DateTime(item.Year, item.Month, 1);
                // pl.ActiveSpendAccountNum = itemPurchasingData.Where(w => w.DateTime.CompareTo(item)<=0 && w.DateTime.CompareTo(firstDate)>=0).Select(s => s.CharacterId).Distinct().Count().ToString();
                pl.ActiveSpendAccountNum = filledCardData.Where(w => w.FILLTIME.CompareTo(item) <= 0 && w.FILLTIME.CompareTo(firstDate) >= 0).Select(s => s.ACCOUNT).Distinct().Count().ToString();
                if (goldSaveData != null && goldSaveData.Count > 0)
                {
                    var ignTemp = goldSaveData.Where(w => w.Date.Equals(item)).Select(s => s.GoldS);
                    if (ignTemp != null && ignTemp.Count() > 0)
                    {
                        pl.InventoryGoldNum = ignTemp.FirstOrDefault().ToString();
                    }
                }

                //int goldExchangeSum = int.Parse(consumeData.Where(w => w.OPERTIME.Equals(item)).Select(s => s).Count().ToString());
                //int socredCardSum = int.Parse(filledCardData.Where(w => w.FILLTIME.Equals(item)).Select(s => s).Count().ToString());
                //pl.InventoryScoreCardNum = (socredCardSum - goldExchangeSum).ToString();
                if (scoredCardSaveData != null && scoredCardSaveData.Count > 0)
                {
                    var scdTemp = scoredCardSaveData.Where(w => w.Date.Equals(item)).Select(s => s.PointSave);
                    if (scdTemp != null && scdTemp.Count() > 0)
                    {
                        pl.InventoryScoreCardNum = scdTemp.FirstOrDefault().ToString();
                    }
                }

                pl.RechargeSum = filledCardData.Where(w => w.FILLTIME.Equals(item)).Select(s => s.POINT).Sum(s => int.Parse(s)).ToString();// point đơn vị là phiếu điểm
                //select sum(`change`) from GOLDSHOPVN0320150312 where objname='Vàng' and srcid = 0 and baseid=65534 and tmpInfo='-' and type=1 AND srcid = 0 AND action = 'Nhấp Thẻ Đổi Vàng'
                //pl.GoldSumExchangeFromScoredCard = consumeData.Where(w => w.OPERTIME.Equals(item)).Select(s => s.CONSUMEPOINT).Sum(s => int.Parse(s)).ToString();// CONSUMEPOINT đơn vị là phiếu điểm
                //pl.GoldSumExchangeFromScoredCard = goldShopData.Where(w => w.ItemName.Equals("Vàng") && w.ScrId.Equals("0") && w.BaseId.Equals("65534") && w.TmpInfo.Equals("-") && w.Type.Equals("1") && w.ScrId.Equals("0") && w.Action.Equals("'Nhấp Thẻ Đổi Vàng") &&   w.DateTime.Equals(item) ).Sum(s => int.Parse(s.PurchasingNum)).ToString();
                pl.GoldSumExchangeFromScoredCard = goldExchangeFromScoredCardData.Where(w => w.DateTime.Equals(item)).Sum(s => int.Parse(s.PurchasingNum)).ToString();
                //select sum(`change`) from GOLDSHOPVN0320150312 where objname='Vàng' and srcid = 0 and baseid=65534 and type=0 and dstid=0
                //pl.UsingGoldNum= goldShopData.Where(w => w.ItemName.Equals("Vàng") && w.ScrId.Equals("0") && w.BaseId.Equals("65534") && w.Type.Equals("0") && w.CharacterId.Equals("0") && w.DateTime.Equals(item)).Sum(s=>int.Parse(s.PurchasingNum)).ToString();
                pl.UsingGoldNum = usingGoldSumData.Where(w => w.DateTime.Equals(item)).Sum(s => int.Parse(s.PurchasingNum)).ToString();
                pl.UsingActualCollect = (double.Parse(pl.UsingGoldNum) / 50).ToString();
                //var itemAndPurshingNumList=(from tb in itemConsumingData group tb by new {tb.ItemName} into grp select new {grp.Key.ItemName,Sum=grp.Sum(s=>int.Parse(s.PurchasingNum))}).ToList();

                var filledCardAtTime = filledCardData.Where(w => w.FILLTIME.Equals(item)).Select(s => s.ACCOUNT).Distinct().ToList();

                pl.RechardAccountNum = filledCardAtTime.Count().ToString();


                int sum = 0;
                foreach (var item2 in filledCardAtTime)
                {
                    int count = filledCardData.Where(w => w.FILLTIME.CompareTo(item) < 0 && w.ACCOUNT.Equals(item2)).Count();
                    if (count <= 0)
                    {
                        sum++;
                    }
                }
                pl.NewIncreaseRechardAccountNum = sum.ToString();

                pl.UsingCharacterNum = itemPurchasingData.Where(w => w.DateTime.Equals(item)).Select(s => s.CharacterId).Distinct().Count().ToString();

                pl.RechardAccountSum = filledCardData.Where(w => w.FILLTIME.CompareTo(item) <= 0).Select(s => s.ACCOUNT).Distinct().Count().ToString();

                var checkDayN = (from tb in timeList.AsParallel() where tb.CompareTo(item) > 0 select tb).Count().ToString();
                if (int.Parse(checkDayN) >= 7)
                {
                    DateTime atSevenDay = item.AddDays(7);

                    var accountAtTimeList = auData.Where(w => w.DateTime.Equals(item)).Select(s => s.Account).Distinct().ToList();
                    int SevenDaySaveAccountNum = 0;
                    foreach (var account in accountAtTimeList)
                    {
                        int count = auData.Where(w => w.Account.Equals(account) && w.DateTime.Equals(atSevenDay)).Count();
                        if (count > 0)
                        {
                            SevenDaySaveAccountNum++;
                        }
                    }
                    pl.SevenDaySaveAccountNum = SevenDaySaveAccountNum.ToString();

                    if (accountAtTimeList.Count() != 0)
                    {
                        pl.SavePercent = ((double)(SevenDaySaveAccountNum * 100) / (double)accountAtTimeList.Count()).ToString("0.00") + "%";
                    }

                }




                //var checkDayN2 = (from tb in timeList.AsParallel() where tb.CompareTo(item) > 0 select tb).Count().ToString();
                if (int.Parse(checkDayN) >= 7)
                {
                    DateTime atSevenDay = item.AddDays(7);

                    List<string> nauList = new List<string>();

                    #region accountAtTimeList
                    //var accountAtTimeList = (from tb in auList where tb.Timestamp.Equals(timeList[i]) select new { tb.Account }).Distinct().ToList();
                    var accountAtTimeList2 = new[] { new { Account = "" } }.ToList();
                    accountAtTimeList2.RemoveAt(0);
                    foreach (var auItem in auData)
                    {
                        if (auItem.DateTime.Equals(item))
                        {
                            accountAtTimeList2.Add(new { Account = auItem.Account });
                        }
                    }
                    accountAtTimeList2 = accountAtTimeList2.Distinct().ToList();

                    #endregion

                    var accountBeforeAtTimeList = (from tb in auData.AsParallel() where tb.DateTime.CompareTo(item) < 0 select new { tb.Account }).Distinct().ToList();

                    if (accountBeforeAtTimeList.Count() != 0)
                    {
                        foreach (var accountItem in accountAtTimeList2)
                        {
                            int numLoop = 0;
                            for (int k = 0; k < accountBeforeAtTimeList.Count(); k++)
                            {
                                if (accountItem.Account.Equals(accountBeforeAtTimeList[k].Account))
                                {
                                    break;
                                }
                                else
                                {
                                    numLoop++;
                                }

                            }
                            if (numLoop == accountBeforeAtTimeList.Count())
                            {
                                nauList.Add(accountItem.Account);
                            }
                        }

                    }
                    else
                        if (accountBeforeAtTimeList.Count() == 0)
                        {
                            foreach (var itemTemp in accountAtTimeList2)
                            {
                                nauList.Add(itemTemp.Account);
                            }
                        }
                    nauList = nauList.Distinct().ToList();

                    int NewIncreaseAccountLossSum = 0;
                    foreach (var account in nauList)
                    {
                        int count = auData.Where(w => w.Account.Equals(account) && w.DateTime.Equals(atSevenDay)).Count();
                        if (count <= 0)
                        {
                            NewIncreaseAccountLossSum++;
                        }
                    }
                    pl.NewIncreaseAccountLossSum = NewIncreaseAccountLossSum.ToString();
                    if (nauList.Count() != 0)
                    {
                        pl.NewIncreaseAccountLossPercent = ((double)(NewIncreaseAccountLossSum * 100) / (double)nauList.Count()).ToString("0.00") + "%";
                    }

                }


                pl.Date = item.ToString("dd/MM/yyyy");

                plList.Add(pl);
            }



            //string startTime = timeList[0];

            //DataTable dt = ConverterWorkData.ToDataTable(plList, 3, 0);

            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Thời gian",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số tài khoản tiêu phí active tháng",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số vàng tồn kho hiện tại",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số phiếu điểm tồn kho trong ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng phiếu điểm mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Số vàng đổi từ phiếu điểm mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                      UIHelpers.CreateTableCell("Số GOSU Nạp vào",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Số vàng tiêu hao mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                      UIHelpers.CreateTableCell("Thực thu tiêu hao mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
            UIHelpers.CreateTableCell("Số tài khoản nạp trẻ trong ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
            UIHelpers.CreateTableCell("Số tài khoản nạp trẻ tăng mới mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                UIHelpers.CreateTableCell("Số nhân vật tiêu hao trong ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng tài khoản nạp thể đến hiện tại",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số tài khoản lưu lại 7 ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tỷ lệ lưu lại",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Số thất thoát của tài khoản tăng mới",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                      UIHelpers.CreateTableCell("Tỷ lệ thất thoát của tài khoản tăng mới",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader")
                }
            );

            table.Rows.Add(rowHeader);
            try
            {
                if (plList == null || plList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    foreach (var dr in plList)
                    {
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Date),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.ActiveSpendAccountNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.InventoryGoldNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.InventoryScoreCardNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.RechargeSum),HorizontalAlign.Center,css),
                                     UIHelpers.CreateTableCell(Converter.ToString(dr.RechargeSumGosu),HorizontalAlign.Center,css),
                                     UIHelpers.CreateTableCell(Converter.ToString(dr.GoldSumExchangeFromScoredCard),HorizontalAlign.Center,css),
                                      UIHelpers.CreateTableCell(Converter.ToString(dr.UsingGoldNum),HorizontalAlign.Center,css),
                                      UIHelpers.CreateTableCell(Converter.ToString(dr.UsingActualCollect),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.RechardAccountNum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.NewIncreaseRechardAccountNum),HorizontalAlign.Center,css),
                         UIHelpers.CreateTableCell(Converter.ToString(dr.UsingCharacterNum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.RechardAccountSum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.SevenDaySaveAccountNum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.SavePercent),HorizontalAlign.Center,css),
                         UIHelpers.CreateTableCell(Converter.ToString(dr.NewIncreaseAccountLossSum),HorizontalAlign.Center,css),
                          UIHelpers.CreateTableCell(Converter.ToString(dr.NewIncreaseAccountLossPercent),HorizontalAlign.Center,css)
                        }
                        );

                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }


        protected void buttonCountSavePL_Click(object sender, EventArgs e)
        {
            dataPanel.Controls.Clear();
            dataPanel.Controls.Add(getPremiumLoginTable());
        }

        protected void buttonCountPL_Click(object sender, EventArgs e)
        {
            dataPanel.Controls.Clear();
            dataPanel.Controls.Add(getFastPremiumLogin());
        }

        private Table getFastPremiumLogin()
        {
            DateTime dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            PremiumLoginWebDB plwebdb = new PremiumLoginWebDB();
            List<PremiumLogin> plList = new List<PremiumLogin>();
            plList = plwebdb.getFastPremiumLoginData(dtFormDate.ToString("yyyy-MM-dd" + " 00:00:00"), dtToDate.ToString("yyyy-MM-dd" + " 23:59:59")).OrderByDescending(o => o.Date).ToList();
            plList = plList.OrderByDescending(o => o.Ticks).ToList();
            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Thời gian",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng tài khoản tồn kho",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số tài khoản tiêu phí active tháng",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số vàng tồn kho hiện tại",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số phiếu điểm tồn kho trong ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng phiếu điểm mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Số GOSU Nạp vào",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số vàng đổi từ phiếu điểm mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                      UIHelpers.CreateTableCell("Số vàng tiêu hao mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                      UIHelpers.CreateTableCell("Thực thu tiêu hao mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
            UIHelpers.CreateTableCell("Số tài khoản nạp trẻ trong ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
            UIHelpers.CreateTableCell("Số tài khoản nạp trẻ tăng mới mỗi ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                UIHelpers.CreateTableCell("Số nhân vật tiêu hao trong ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng tài khoản nạp thể đến hiện tại",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số tài khoản lưu lại 7 ngày",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tỷ lệ lưu lại",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Số thất thoát của tài khoản tăng mới",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                      UIHelpers.CreateTableCell("Tỷ lệ thất thoát của tài khoản tăng mới",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader")
                }
            );

            table.Rows.Add(rowHeader);
            try
            {
                if (plList == null || plList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    foreach (var dr in plList)
                    {
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Date),HorizontalAlign.Center,css),
                                      UIHelpers.CreateTableCell(Converter.ToString(dr.InventoryAccountSum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.ActiveSpendAccountNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.InventoryGoldNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.InventoryScoreCardNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.RechargeSum),HorizontalAlign.Center,css),
                                     UIHelpers.CreateTableCell(Converter.ToString(dr.RechargeSumGosu),HorizontalAlign.Center,css),
                                     UIHelpers.CreateTableCell(Converter.ToString(dr.GoldSumExchangeFromScoredCard),HorizontalAlign.Center,css),
                                      UIHelpers.CreateTableCell(Converter.ToString(dr.UsingGoldNum),HorizontalAlign.Center,css),
                                      UIHelpers.CreateTableCell(Converter.ToString(dr.UsingActualCollect),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.RechardAccountNum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.NewIncreaseRechardAccountNum),HorizontalAlign.Center,css),
                         UIHelpers.CreateTableCell(Converter.ToString(dr.UsingCharacterNum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.RechardAccountSum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.SevenDaySaveAccountNum),HorizontalAlign.Center,css),
                        UIHelpers.CreateTableCell(Converter.ToString(dr.SavePercent),HorizontalAlign.Center,css),
                         UIHelpers.CreateTableCell(Converter.ToString(dr.NewIncreaseAccountLossSum),HorizontalAlign.Center,css),
                          UIHelpers.CreateTableCell(Converter.ToString(dr.NewIncreaseAccountLossPercent),HorizontalAlign.Center,css)
                        }
                        );

                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }
    }
}